import { createApp } from 'vue'
import './style.css'
import './assets/styles/theme.css'
import App from './App.vue'
import router from './router'
import { setupStore } from './stores'
import { useUserStore } from '@/stores/user'

// TinyMCE
import 'tinymce/tinymce.min.js'
import 'tinymce/themes/silver/theme.min.js'
import 'tinymce/icons/default/icons.min.js'
// TinyMCE语言包将在组件中动态加载
// TinyMCE 插件
import 'tinymce/plugins/advlist'
import 'tinymce/plugins/autolink'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/link'
import 'tinymce/plugins/image'
import 'tinymce/plugins/charmap'
import 'tinymce/plugins/preview'
import 'tinymce/plugins/anchor'
import 'tinymce/plugins/searchreplace'
import 'tinymce/plugins/visualblocks'
import 'tinymce/plugins/code'
import 'tinymce/plugins/fullscreen'
import 'tinymce/plugins/insertdatetime'
import 'tinymce/plugins/media'
import 'tinymce/plugins/table'
import 'tinymce/plugins/help'
import 'tinymce/plugins/wordcount'

// Element Plus
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// Ant Design Vue
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'

// NProgress
import 'nprogress/nprogress.css'

// 被动事件监听器优化
import { setupGlobalPassiveEvents } from '@/utils/passive-events'

// 设置全局被动事件监听器（在创建应用之前）
setupGlobalPassiveEvents()

const app = createApp(App)

// 注册 Element Plus 图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 设置状态管理
setupStore(app)

// 使用路由
app.use(router)
app.use(ElementPlus)
app.use(Antd)

// 初始化用户状态并挂载应用
;(async () => {
  const userStore = useUserStore()
  // 确保初始化完成后再挂载应用，避免初始化过程中的状态不一致
  try {
    await userStore.initUserState()
  } catch (error) {
    console.error('初始化用户状态失败:', error)
    // 即使初始化失败，也继续挂载应用，让用户看到错误页面
  }
  
  app.mount('#app')
})()